package xiaobange.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import xiaobange.common.CustomException;
import xiaobange.common.R;
import xiaobange.dto.SetmealDto;
import xiaobange.entity.Setmeal;
import xiaobange.entity.SetmealDish;
import xiaobange.mapper.SetmealMapper;
import xiaobange.service.SetmealService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImp extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishServiceImpl setmealDishService;


    /**
     * 添加套餐
     * @param setmealDto
     */
    @Transactional
    @Override
    public void Insert(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除
     * @param ID
     */
    @Transactional
    @Override
    public void DeleteByID(Long[] ID){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId , ID);
        lambdaQueryWrapper.eq(Setmeal::getStatus , 1);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0){
            throw new CustomException("删除套餐中存在上架售中，请先下架再删除！");
        }

        //根据ID批量删除
        this.removeByIds(Arrays.asList(ID));

        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getSetmealId , ID);
        //根据表达式查询出来的数据删除
        setmealDishService.remove(dishLambdaQueryWrapper);
    }

    /**
     * 修改数据回显
     * @param ID
     * @return
     */
    @Override
    public SetmealDto UpdateShow(Long ID) {
        Setmeal setmeal = this.getById(ID);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId , setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改信息提交
     * @param setmealDto
     */
    @Transactional
    @Override
    public void UpdateSubmit(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId , setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
         setmealDishes = setmealDishes.stream().map((date) -> {
            date.setSetmealId(setmealDto.getId());
            return date;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }


}
